local hx__liyu = fk.CreateSkill {
  name = "hx__liyu",
}


hx__liyu:addEffect(fk.Damage, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return
      target == player and
      player:hasSkill(hx__liyu.name) and
      data.to ~= player and
      data.card and
      data.card.trueName == "slash" and
      not data.to:isAllNude() and
      data.to:isAlive()
  end,
  on_cost = function(self, event, target, player, data)
    local cards = player.room:askToChooseCards(player,{
      min = 0,
      max = data.damage,
      target = data.to,
      flag = "hej",
      prompt = "#hx__liyu-prey::" .. data.to.id .. ":" .. data.damage,
      skill_name = hx__liyu.name,
      cancelable = true,
    })
    if #cards > 0 then
      event:setCostData(self, cards)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    ---@type string
    local skillName = hx__liyu.name
    local room = player.room
    room:notifySkillInvoked(player, skillName, "offensive")
    player:broadcastSkillInvoke(skillName, math.random(1, 2))

    local cards = event:getCostData(self)
    room:obtainCard(player, cards, nil, fk.ReasonPrey, player, skillName)
    local victim = data.to
    if not victim:isAlive() then return false end

    local cardsDrawn = victim:drawCards(#cards, skillName)
    if not victim:isAlive() then return false end

    table.insertTable(cards, cardsDrawn)
    local types = {}
    for _, id in ipairs(cards) do
      table.insertIfNeed(types, Fk:getCardById(id).type)
      if #types > 2 then
        break
      end
    end
    if #types > 2 then
      local duelTargets = {}
      local duel = Fk:cloneCard("duel")
      for _, p in ipairs(room:getOtherPlayers(victim, false)) do
        if p ~= player and player:canUseTo(duel, p) then
          table.insert(duelTargets, p)
        end
      end
      if #duelTargets ~= 0 then
        player:broadcastSkillInvoke(skillName, math.random(1, 2))
        local tos = room:askToChoosePlayers(victim,{
          min_num = 1,
          max_num = 1,
          targets = duelTargets,
          skill_name = skillName,
          prompt = "#hx__liyu-duel:" .. player.id,
          cancelable = false,
        })
        room:useCard{
          from = player,
          tos = tos,
          card = duel,
          extraUse = false,
        }
      end
    end
  end,
})


return hx__liyu
